//高亮
$.fn.highlight = function () {
    var obj = this;
    Do.ready('prettify', function () {
        $(obj).addClass("prettyprint");
        prettyPrint();
    });
};

//文本框插入
$.fn.insertText = function (myValue) {
    var $t = $(this)[0];
    if (document.selection) {
        this.focus();
        sel = document.selection.createRange();
        sel.text = myValue;
        this.focus();
    } else if ($t.selectionStart || $t.selectionStart == '0') {
        var startPos = $t.selectionStart;
        var endPos = $t.selectionEnd;
        var scrollTop = $t.scrollTop;
        $t.value = $t.value.substring(0, startPos) + myValue + $t.value.substring(endPos, $t.value.length);
        this.focus();
        $t.selectionStart = startPos + myValue.length;
        $t.selectionEnd = startPos + myValue.length;
        $t.scrollTop = scrollTop;
    } else {
        this.value += myValue;
        this.focus();
    }
};

//编辑器调用
$.fn.bbsEditor = function (options) {
    var defaults = {
        config: {}
    }
    var options = $.extend(defaults, options);

    function ubb_url() {
        var str = prompt("请输入URL", "http://");
        if (str != null && str != "") {
            return '[url]' + str + '[/url]';
        }
    }

    function ubb_image() {
        var str = prompt("请输入图片地址", "http://");
        if (str != null && str != "") {
            return '[img]' + str + '[/img]';
        }
    }

    function ubb_size() {
        var str = prompt("请输入文字大小，单位：像素", "14");
        if (str != null && str != "") {
            return '[size=' + str + ']输入文字[/size]';
        }
    }

    function ubb_flash() {
        var str = prompt("请输入flash地址", "http://");
        if (str != null && str != "") {
            return '[swf]' + str + '[/swf]';
        }
    }

    function ubb_code() {
        return '[code]在此输入代码[/code]';
    }

    this.each(function () {
        var editor = this;
        var tool = ['url', 'image', 'size', 'flash', 'code'];
        var toolID = $(this).attr('id') + '_editor';
        var html = '\
                    <div class="dux-editor">\
                        <a class="ins-url icon-link"></a>\
                        <a class="ins-image icon-picture-o"></a>\
                        <a class="ins-size icon-font"></a>\
                        <a class="ins-flash icon-film"></a>\
                        <a class="ins-code icon-code"></a>\
                    </div>\
            ';
        $(editor).before(html);
        for (var v in tool) {
            var button = tool[v];
            bind_click(button);
        }

        function bind_click(button) {
            var obj = $(editor).prev('.dux-editor').find('.ins-' + button);
            obj.click(function () {
                var value = eval('ubb_' + button + '()');
                if (value != '' && value != null) {
                    $(editor).insertText(value);
                }
            });
        }
    });
};